$(function() {
	
	$("#wetlandAnimal_add_importantanimal").click(function() {
		wetlandAnimal_add_importantanimal();
	});
	
	$("#wetlandAnimal_add_alien").click(function() {
		wetlandAnimal_add_alien();
	});
	$("#wetlandAnimal_add_sub").click(function() {
		wetlandAnimal_add_sub();
	});
	getAnimals();
});
var wetlandAnimal_add_import_i = 2;
function wetlandAnimal_add_importantanimal(){
	var tr = $('<tr>'+
			'<td><input type="text" readonly="readonly" name="proa_clazz_'+wetlandAnimal_add_import_i+'"/></td>'+
			'<td><input type="text" readonly="readonly" name="proa_cn_name_'+wetlandAnimal_add_import_i+'"/></td>'+
			'<td><input type="text" readonly="readonly" name="proa_en_name_'+wetlandAnimal_add_import_i+'"/></td>'+
			'<td><input type="text" readonly="readonly" name="proa_latin_name_'+wetlandAnimal_add_import_i+'"/></td>'+
			'<td><input type="text" readonly="readonly" name="proa_IUCN_standard_'+wetlandAnimal_add_import_i+'"/></td>'+
			'<td><input type="text" readonly="readonly" name="proa_CITES_standard_'+wetlandAnimal_add_import_i+'"/></td>'+
			'<td><input type="text" readonly="readonly" name="proa_CMS_standard_'+wetlandAnimal_add_import_i+'"/></td>'+
			'<td><input type="text" readonly="readonly" name="proa_protection_level_'+wetlandAnimal_add_import_i+'"/></td>'+
			'<td class="text-center">'+
				'<a class="green" style="cursor: pointer;" title="编辑">'+
						'<i class="ace-icon fa fa-pencil bigger-130"></i>'+
				'</a>　'+
				'<a class="red" style="cursor: pointer;" title="删除" onclick="wetlandAnimal_del($(this))">'+
						'<i class="ace-icon fa fa-trash-o bigger-130"></i>'+
				'</a>'+
			'</td>'+
		'</tr>');
		$("#importantanimal_tbody").append(tr);
		
		wetlandAnimal_add_import_i ++;
	
}


var wetlandAnimal_add_alien_i = 2;
function wetlandAnimal_add_alien(){
	var tr = $('<tr>'+
			'<td><input type="text" readonly="readonly" name="alea_clazz_'+wetlandAnimal_add_alien_i+'"/></td>'+
			'<td><input type="text" readonly="readonly" name="alea_cn_name_'+wetlandAnimal_add_alien_i+'"/></td>'+
			'<td><input type="text" readonly="readonly" name="alea_en_name_'+wetlandAnimal_add_alien_i+'"/></td>'+
			'<td><input type="text" readonly="readonly" name="alea_latin_name_'+wetlandAnimal_add_alien_i+'"/></td>'+
		'<td>'+
			'<input type="radio" readonly="readonly" value="无影响" name="alea_impact_'+wetlandAnimal_add_alien_i+'">&nbsp;<span>无影响</span>&nbsp;&nbsp;'+
			'<input type="radio" readonly="readonly" value="潜在的" name="alea_impact_'+wetlandAnimal_add_alien_i+'">&nbsp;<span>潜在的</span>&nbsp;&nbsp;'+
			'<input type="radio" readonly="readonly" value="轻微影响" name="alea_impact_'+wetlandAnimal_add_alien_i+'">&nbsp;<span>实际存在（轻微影响）</span>&nbsp;&nbsp;'+
			'<input type="radio" readonly="readonly" value="主要影响" name="alea_impact_'+wetlandAnimal_add_alien_i+'">&nbsp;<span>实际存在（主要影响）</span>'+
		'</td>'+
		'<td class="text-center">'+
		'<a class="green" style="cursor: pointer;" title="编辑">'+
				'<i class="ace-icon fa fa-pencil bigger-130"></i>'+
		'</a>　'+
		'<a class="red" style="cursor: pointer;" title="删除" onclick="wetlandAnimal_del($(this))">'+
				'<i class="ace-icon fa fa-trash-o bigger-130"></i>'+
		'</a>'+
	'</td>'+
	'</tr>');
	$('#alienanimal_tbody').append(tr);
	wetlandAnimal_add_alien_i ++;
}
/**
 * 删除行
 * @param delBtn
 */
function wetlandAnimal_del(delBtn){
	delBtn.parent().parent().remove();
}

/**
 * 提交信息
 */
function wetlandAnimal_add_sub(){
	var option = {
			dataType : 'json',
			data : {wetlandParkId : wetlandParkId},
			success : function(value) {
				if (value == true) {
					wetlandAnimal.html(wetlandAnimal.text() + successHTML);
					$.gritter.add({
						title : '确认消息',
						text : '恭喜您,信息添加成功！',
						class_name : 'gritter-success'
					});
				} else {
					wetlandAnimal.html(wetlandAnimal.text() + warnHTML);
					$.gritter.add({
						title : '确认消息',
						text : '对不起,信息添加失败！',
						class_name : 'gritter-error'
					});
				}
			},
			error : function() {
				wetlandAnimal.html(wetlandAnimal.text() + warnHTML);
				$.gritter.add({
					title : '服务器错误',
					text : '对不起，服务器错误，请联系系统管理员！',
					class_name : 'gritter-error'
				});
			}
		};
		$("#wetlandAnimal_form").ajaxForm(option).submit();
}

function getAnimals(){
	var id =$("#wetlandParkId").val();
	$.ajax({
		url : "findbyid",
		dataType : "json",
		type : "POST",
		data : {id:id},
		async : false,
		error : function(error) {
			console.log(error.responseText);

			$.gritter.add({
				title : '服务器错误',
				text : '对不起，服务器错误，请联系系统管理员！',
				class_name : 'gritter-error'
			});

		},
		success : function(strValue) {
			if (strValue != null) {
				if(strValue.wlin_animal_protection.length>=1){
					$("input[name='proa_clazz_1']").val(strValue.wlin_animal_protection[0].clazz);
					$("input[name='proa_cn_name_1']").val(strValue.wlin_animal_protection[0].cn_name);
					$("input[name='proa_en_name_1']").val(strValue.wlin_animal_protection[0].en_name);
					$("input[name='proa_latin_name_1']").val(strValue.wlin_animal_protection[0].latin_name);
					$("input[name='proa_IUCN_standard_1']").val(strValue.wlin_animal_protection[0].iucn_standard);
					$("input[name='proa_CITES_standard_1']").val(strValue.wlin_animal_protection[0].cites_standard);
					$("input[name='proa_CMS_standard_1']").val(strValue.wlin_animal_protection[0].cms_standard);
					$("input[name='proa_protection_level_1']").val(strValue.wlin_animal_protection[0].protection_level);
					for(var i=2;i<strValue.wlin_animal_protection.length+1;i++){
						wetlandAnimal_add_importantanimal();
						$("input[name='proa_clazz_"+i+"']").val(strValue.wlin_animal_protection[i-1].clazz);
						$("input[name='proa_cn_name_"+i+"']").val(strValue.wlin_animal_protection[i-1].cn_name);
						$("input[name='proa_en_name_"+i+"']").val(strValue.wlin_animal_protection[i-1].en_name);
						$("input[name='proa_latin_name_"+i+"']").val(strValue.wlin_animal_protection[i-1].latin_name);
						$("input[name='proa_IUCN_standard_"+i+"']").val(strValue.wlin_animal_protection[i-1].iucn_standard);
						$("input[name='proa_CITES_standard_"+i+"']").val(strValue.wlin_animal_protection[i-1].cites_standard);
						$("input[name='proa_CMS_standard_"+i+"']").val(strValue.wlin_animal_protection[i-1].cms_standard);
						$("input[name='proa_protection_level_"+i+"']").val(strValue.wlin_animal_protection[i-1].protection_level);
					}
				}
				if(strValue.wlin_animal_alien_invasive_species.length>=1){
					$("input[name='alea_clazz_1']").val(strValue.wlin_animal_alien_invasive_species[0].clazz);
					$("input[name='alea_cn_name_1']").val(strValue.wlin_animal_alien_invasive_species[0].cn_name);
					$("input[name='alea_en_name_1']").val(strValue.wlin_animal_alien_invasive_species[0].en_name);
					$("input[name='alea_latin_name_1']").val(strValue.wlin_animal_alien_invasive_species[0].latin_name);
					
					var protection = strValue.wlin_animal_alien_invasive_species[0].impact;
					if(protection!=null){
			
						var protectionlevel = $("input[name='alea_impact_1']");
						for(var i=0;i<protectionlevel.length;i++){
								                        var checkValue=$(protectionlevel[i]).val();
								                        if(protection==checkValue){
								                        	$(protectionlevel[i]).attr("checked",true);
								                        }
						 }
					}
					
					for(var i=2;i<strValue.wlin_animal_alien_invasive_species.length+1;i++){
						wetlandAnimal_add_alien();
						$("input[name='alea_clazz_"+i+"']").val(strValue.wlin_animal_alien_invasive_species[i-1].clazz);
						$("input[name='alea_cn_name_"+i+"']").val(strValue.wlin_animal_alien_invasive_species[i-1].cn_name);
						$("input[name='alea_en_name_"+i+"']").val(strValue.wlin_animal_alien_invasive_species[i-1].en_name);
						$("input[name='alea_latin_name_"+i+"']").val(strValue.wlin_animal_alien_invasive_species[i-1].latin_name);
						var protection = strValue.wlin_animal_alien_invasive_species[i-1].impact;
						if(protection!=null){
							var protectionlevel = $("input[name='alea_impact_"+i+"']");
							for(var j=0;j<protectionlevel.length;j++){
									                        var checkValue=$(protectionlevel[j]).val();
									                        if(protection==checkValue){
									                        	$(protectionlevel[j]).attr("checked",true);
									                        }
							 }
						}
					}
				}
				
				
				
			}
		}
	});
}

